package editor.cn;

import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;

class Solution2848{
    public static void main(String[] args) {
        Solution solution = new Solution2848().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int numberOfPoints(List<List<Integer>> nums) {
        //return hash(nums);
        return diff(nums);
    }

        private int diff(List<List<Integer>> nums) {
            //初始化差分数组
            //如何想到用差分数组来解决的？因为只要是将汽车出现过的位置，全部加1，最后，只需要统计有哪些位置大于即可。这个数目就是坐标轴的交点数量。
            //那么如何将start与end之间快速地+1呢？这里就是差分数组的天下：
            int[] diff = new int[102];//查分数组要比原数组的长度加1，因为右端要覆盖住，就要超1
            for (List<Integer> num : nums) {
                diff[num.get(0)]++;
                diff[num.get(1)+1]--;
            }
            int s=0;
            int count=0;
            for (int i : diff) {
                s+=i;
                if (s>0)count++;
            }
            return count;
        }

        private int hash(List<List<Integer>> nums) {
            int[] hash = new int[101];
            for (int i = 0; i < nums.size(); i++) {
                for (int j = nums.get(i).get(0); j <= nums.get(i).get(1); j++) {
                    if (hash[j]!=1)hash[j]=1;
                }
            }
            return Arrays.stream(hash).sum();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}













